home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / future / convert / misc.m < prev    next >
Encoding:
Text File  |  1990-07-18  |  3.4 KB  |  148 lines

  1. // 
  2. // Miscellaneous support functions for Cassandra
  3. // Copyright (c) 1989, 1990 Jiro Nakamura
  4. //
  5. // misc.m
  6. //
  7. // RCS Information
  8. // Revision Number->    $Revision: 2.3 $
  9. // Last Revised->    $Date: 90/03/28 07:27:48 $
  10. //
  11. static char rcsid[] = "$Header: /User/jiro/Programming/Cassandra/src/RCS/misc.m,v 2.3 90/03/28 07:27:48 jiro Exp Locker: jiro $";
  12.  
  13. #import <stdio.h>
  14. #import "cass.h"
  15. #import <strings.h>
  16. #import <appkit/Panel.h>       /* for NXRunAlertPanel */
  17. #import <sys/file.h>
  18. #import <libc.h>            /* for open(), close(), exit() */
  19. #import "misc.h"
  20. #import "errno.h"
  21.  
  22. // Function:     fileOpen(filename, mode, message)
  23. // Arguments:    char * filename -> name of file to be opened
  24. //        char * mode    -> mode using fopen() (e.g. "r", "a+")
  25. //        char * message    -> message to be shown if fopen() fails
  26. // Description:    Opens the file <filename> with mode <mode>
  27. //        using fopen(). If it is successful, a FILE *
  28. //        is returned. If it fails, fileOpen tries
  29. //        to create the file with creat() mode 0600.
  30. //        If that fails too, it uses NXRunAlertPane()
  31. //        to show <message>, then exit()s the program.
  32. // Returns:    FILE * to open file
  33. FILE *fileOpen(filename,mode,message)
  34. char *filename, *mode, *message;
  35. {
  36.     FILE *fd, *fopen();
  37.  
  38.     #ifdef DEBUG
  39.         fprintf(stderr,"fileOpened <%s> with mode %s\n",
  40.                 filename, mode);
  41.     #endif
  42.  
  43.     if( (fd = fopen(filename, mode)) == NULL)
  44.         {
  45.         int ifd;
  46.  
  47.         fprintf(stderr,"%s: Uh...oh.... I (fileOpen) can't "
  48.                 "access %s with mode %s....\n"
  49.                 "%s: This is not a fatal error, "
  50.                 "I will try to create the file.\n",
  51.                 PROGNAME, filename,mode,
  52.                 PROGNAME);
  53.         
  54.         ifd = open( filename, O_CREAT, 0600);
  55.         close(ifd);
  56.         if( (fd = fopen(filename, mode)) == NULL)
  57.             {
  58.             NXRunAlertPanel("File Error",message,"Quit",NULL,NULL);
  59.             exit(1);
  60.             }
  61.         }
  62.     return fd;
  63. }
  64.  
  65. // Seek file to this position
  66. FILE *fileSeek(source, here, errormsg)
  67. FILE *source;
  68. int here;
  69. char *errormsg;
  70. {    
  71.     #ifdef DEBUG
  72.         fprintf(stderr,"Seeking to %d in fileSeek\n",here);
  73.     #endif
  74.  
  75.  
  76.     if(  here < 0)
  77.         {
  78.         fprintf(stderr,"%s: Error in fileSeek, "
  79.             "cannot seek to negative number %d\n", 
  80.             PROGNAME, here);
  81.         NXRunAlertPanel("File Seek Error", 
  82.             "Cannot seek to a negative number.",
  83.             "OK",NULL,NULL);
  84.         return NULL;
  85.         }
  86.     
  87.     /* Seek to the correct position */
  88.     if( fseek( source, (long) FILE_LEN * here, SEEK_SET) == -1)     
  89.         {        
  90.         if( errno == EIO)
  91.             {
  92.             fprintf(stderr, "%s: I/O Error on read. ", PROGNAME);
  93.             NXRunAlertPanel("Seek error", 
  94.                 "An IO error occured while seeking.",
  95.                 "OK",NULL,NULL);
  96.             return NULL;
  97.             }
  98.  
  99.         /* If we can't seek to the end because the file */
  100.         /* is too short, then */
  101.         /* seek to the end of file and add newlines*/            
  102.         fseek(source, 0L, SEEK_END);        
  103.         do
  104.             {
  105.             #ifdef DEBUG
  106.                 fprintf(stderr,"Looping: ftell(source) = "
  107.                     "%d \n",ftell(source));
  108.             #endif
  109.  
  110.             fputc('\n', source);
  111.             }
  112.         while( ftell(source) < FILE_LEN * here + 5);
  113.         
  114.         /* Try again */
  115.         if( fseek( source, (long) FILE_LEN * here, 0) == -1)         
  116.             {
  117.             fprintf(stderr, 
  118.             "%s: We have an error fseek()-ing to %d....\n"
  119.             "%s: Error Numbers are as follows: errno = %d, EIO =%d"
  120.             "%s: Calling procedure says: %s\n",
  121.             PROGNAME, here, 
  122.             PROGNAME, errno, EIO,
  123.             PROGNAME, errormsg);
  124.  
  125.             NXRunAlertPanel( "Seek error", errormsg, 
  126.                 "Quit", NULL, NULL);
  127.             fclose(source);
  128.             exit(1);
  129.             }
  130.         }
  131.     return source;
  132. }
  133.  
  134. //
  135. // fgetnum - read in a number into int format
  136. //
  137. int fgetnum(source)
  138. FILE *source;
  139. {
  140.     int atoi();
  141.     char str[20];
  142.     
  143.     fgets(str, 20, source);
  144.     return( atoi(str));
  145. }
  146.  
  147.  
  148.